//
//  HEReagentCheckDetailsController.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/12/23.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEReagentCheckDetailsController.h"
#import "HECheckDetailModel.h"
#import "HECheckTitleCell.h"
#import "HECheckItemCell.h"
#import "HECheckFooterCell.h"
#import "HECheckResCell.h"
#import "HECusCheckItemModel.h"
#import "HECheckFooterCell.h"

@interface HEReagentCheckDetailsController ()<UITableViewDelegate, UITableViewDataSource>
 
@property (nonatomic, strong) GDTableView *tableView;
@property (nonatomic, strong) NSMutableArray * dataSourceArr;
@property (nonatomic, strong) HECheckDetailModel *details;
@end

@implementation HEReagentCheckDetailsController

- (NSMutableArray *)dataSourceArr {
    if (!_dataSourceArr) {
        _dataSourceArr = [NSMutableArray array];
    }
    return _dataSourceArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"报告详请";
    
    [self.view addSubview:self.tableView];
    
    [self loadData];
}

- (GDTableView *)tableView {
   if (!_tableView) {
       _tableView = [[GDTableView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight , KScreenWidth, KScreenHeight - ZXNavBarHeight) style:UITableViewStyleGrouped];
       _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
       _tableView.scrollsToTop = YES;
       _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.01)];
       _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.01)];

       _tableView.showsVerticalScrollIndicator = NO;
       _tableView.backgroundColor = CViewBgColor;
       _tableView.delegate = self;
       _tableView.dataSource = self;
       
       [_tableView registerClass:[HECheckItemCell class] forCellReuseIdentifier: KHECheckItemCell];
       [_tableView registerClass:[HECheckResCell class] forCellReuseIdentifier: KHECheckResCell];
       [_tableView registerClass:[HECheckTitleCell class] forCellReuseIdentifier: KHECheckTitleCell];
       [_tableView registerClass:[HECheckFooterCell class] forCellReuseIdentifier: KHECheckFooterCell];
   }
   return _tableView;
}
 

- (void)loadData {

    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:KcheckDetail parameters:@{@"id":[NSNumber numberWithInteger:_itemId]} success:^(id  _Nonnull responseObject) {
        NSLog(@"response：  %@", [responseObject mj_JSONString]);
        HEArrayResponse *response = [HEArrayResponse mj_objectWithKeyValues:responseObject];

        if (response.data.count > 0) {
            self.details = [HECheckDetailModel mj_objectWithKeyValues:response.data.firstObject];
            [self.dataSourceArr addObject:self.details];
            
            for (HECheckBodyItemModel *item in self.details.body.items) {
                NSMutableArray *sec = @[].mutableCopy;
                
                if (![DYTools isBlankString:item.desc]) {
                    HECusCheckItemModel *model = [HECusCheckItemModel new];
                    model.title = @"临床意义：";
                    model.content = item.desc;
                    [sec addObject:model];
                }
                
                if (![DYTools isBlankString:item.projectValue]) {
                    HECusCheckItemModel *model = [HECusCheckItemModel new];
                    model.title = @"结果数值：";
                    model.content = [NSString stringWithFormat:@"%@%@ %@" , item.projectValue, IsNotNull(item.projectUnit), IsNotNull(item.projectSymbol)];
                    model.contentColor = item.abnormal ? CPriceColor : CNavLightBlackColor;
                    [sec addObject:model];
                }
                
                if (![DYTools isBlankString:item.projectSymbolName]) {
                    HECusCheckItemModel *model = [HECusCheckItemModel new];
                    model.title = @"检测结果：";
                    model.content = item.projectSymbolName;
                    
                    if (item.abnormal) {
                        model.content = [NSString stringWithFormat:@"%@（异常）", model.content];
                        model.contentColor = CPriceColor;
                    }
                    
                    [sec addObject:model];
                }
                
                if (![DYTools isBlankString:item.refer]) {
                    HECusCheckItemModel *model = [HECusCheckItemModel new];
                    model.title = @"参考结果：";
                    model.content = item.refer;
                    [sec addObject:model];
                }
                
                if (![DYTools isBlankString:item.diseaseDescription]) {
                    HECusCheckItemModel *model = [HECusCheckItemModel new];
                    model.title = @"异常参考：";
                    model.content = item.diseaseDescription;
                    [sec addObject:model];
                }
                
                if (![DYTools isBlankString:item.abnormalRefer]) {
                    HECusCheckItemModel *model = [HECusCheckItemModel new];
                    model.title = @"病理描述：";
                    model.content = item.abnormalRefer;
                    [sec addObject:model];
                }
                [self.dataSourceArr addObject: sec];
            }
            
        }
    
        [self.tableView reloadData];
 
    } failure:^(NSError * _Nonnull error) {
    }];
}



#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSourceArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }else {
        NSArray *arr = self.dataSourceArr[section];
        return arr.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        HECheckResCell *cell = [_tableView dequeueReusableCellWithIdentifier: KHECheckResCell];
        [cell configCellWithModel:self.dataSourceArr[indexPath.row]];
        return cell;
    }else {
        NSArray *items = self.dataSourceArr[indexPath.section];

        HECheckItemCell *cell = [_tableView dequeueReusableCellWithIdentifier: KHECheckItemCell];
        [cell configCellWithModel: items[indexPath.row]];
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *cellId = @"";
    if (indexPath.section == 0) {
        cellId = KHECheckResCell;
        return [tableView fd_heightForCellWithIdentifier: cellId cacheByIndexPath:indexPath configuration:^(MCTableBaseCell *cell) {
            [cell configCellWithModel:self.dataSourceArr[indexPath.row]];
        }];
    }else {
        cellId = KHECheckItemCell;
        NSArray *items = self.dataSourceArr[indexPath.section];

        return [tableView fd_heightForCellWithIdentifier: cellId cacheByIndexPath:indexPath configuration:^(MCTableBaseCell *cell) {
            [cell configCellWithModel: items[indexPath.row]];
        }];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return CGFLOAT_MIN;
    }
    return 45;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 0) {
        return CGFLOAT_MIN;
    }
    return 30;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return [UIView new];
    }else {
        HECheckTitleCell *cell = [_tableView dequeueReusableCellWithIdentifier: KHECheckTitleCell];
        
        HECheckBodyItemModel *item = self.details.body.items[section-1];
        [cell configCellWithModel: [NSString stringWithFormat:@"%@（%@）", item.projectName, item.projectShortName]];
        return cell;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 0) {
        return [UIView new];
    }else {
        HECheckFooterCell *cell = [_tableView dequeueReusableCellWithIdentifier: KHECheckFooterCell];
        return cell;
    }
}

@end
